import axios from 'axios'
import Message from 'ant-design-vue/lib/message';
import 'ant-design-vue/lib/message/style/css';
import '../../public/config'

const apiUrl=window.g.apiUrl
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    let token=localStorage.getItem('mtoken')
    // config.timeout=10000
    config.headers.common['Authorization'] = token?token:'';
    return config;
}, function (error) {
// 对请求错误做些什么
    return Promise.reject(error);
});
// 响应拦截器
axios.interceptors.response.use(    
    response => {   
        // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据     
        // 否则的话抛出错误
        if (response.status === 200) {            
            return Promise.resolve(response);        
        } else {            
            return Promise.reject(response);        
        }    
    },    
    error => {            
        if (error.response.status) {            
            switch (error.response.status) {                
                // 404请求不存在
                case 404:
                    Message.error('请求不存在')
                    break;
            }
            return Promise.reject(error.response);
        }
    }    
)
export function get(url, params){    
    return new Promise((resolve, reject) =>{        
        axios.get(apiUrl+url, {            
            params: params        
        }).then(res => {
            resolve(res.data);
        }).catch(err =>{
            reject(err.data)        
    })    
});}
export function post(url, params) {
    return new Promise((resolve, reject) => {
         axios.post(apiUrl+url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err =>{
            reject(err.data)
        })
    });
}
